var app = getApp();
Page({
    data: {
        chooseAddr: {},
        goods:[],
        addrShow: false,
        scrollTop: 100,
        total: 0
    },
     
    myaddrChange: function () {//触摸选择地址
      wx.navigateTo({
        url: '../myAddr/myAddr?choose=true'
    })

    },
    
    toSelect: function (e) {//选中地址
        var _this = this;
        var id = e.currentTarget.dataset.aid;
        _this.setData({selectedID: id});
        for (var i = 0; i < _this.data.addresslist.length; i++) {
            if (_this.data.addresslist[i].id == id) {
                _this.setData({
                    "oinfo.City": _this.data.addresslist[i].city,
                    "oinfo.District": _this.data.addresslist[i].area,
                    "oinfo.Consignee": _this.data.addresslist[i].name,
                    "oinfo.Cellphone": _this.data.addresslist[i].phone,
                    "oinfo.Address": _this.data.addresslist[i].address,
                    addr: _this.data.addresslist[i].city + ' ' + _this.data.addresslist[i].area + ' ' + _this.data.addresslist[i].address,
                    addrShow: false
                });
                break;
            }
        }
    },
    onLoad: function (options) {
        var _this = this;
        const goods = JSON.parse(decodeURIComponent(options.goods));
        console.log('goods', goods);
        _this.setData({
          goods: goods
      });
      _this.getTotalPrice();
        _this.getAddressList();
        console.log(this.data.plist);
    },
    getAddressList: function () {

    },
    onShow: function (e) {

    },

    getTotalPrice: function () {//应付金额
      let totalPrice = 0;
      let checkboxItems = this.data.goods;
      let arr = [];
      checkboxItems.forEach((i) => {
          totalPrice += parseFloat(i.price) * i.count;
      });
      this.setData({
        total: totalPrice.toFixed(2)
      });
      console.log(this.data.checkAll);
    },
    getProductList: function () {
        var _this = this;
        var arr_pro = [];
        var pl = _this.data.plist;//name: p.name, price: p.price, size: p.size, num: p.num, brand: p.brand,supplyno
        for (var i = 0; i < pl.length; i++) {
            arr_pro.push({
                ProductName: pl[i].name,
                Price: pl[i].price,
                Size: pl[i].size,
                Num: pl[i].num,
                CakeNo: 0,
                OType: 0,
                IType: 0,
                SupplyNo: pl[i].supplyno,
                //生日内容
                IsCutting: 0,
                CutNum: 0,
                BrandCandleType: 0,
                Remarks: '',
                Premark: null,//生产备注
            });
        }
        return arr_pro;
    },
    valid: function () {
        var _this = this;
        var err = "";
        if (!_this.data.chooseAddr.id) {
            err = "请选择收货人信息！";
            wx.showModal({
                showCancel: false,
                title: '',
                content: err
            })
            return false;
        }
        
        return true;
    },

    bindTextAreaBlur(e) {
        console.log(e.detail.value)
    },

    submit: function () {
        var that = this;
        if(!that.valid()){
            return;
        }
        var goodlist = [];
        that.data.goods.forEach((item, index) => {
            var good = {};
            good.id = item.id;
            good.num = item.count;
            goodlist.push(good);
        });
        console.log(goodlist);
        wx.request({
            url: app.d.hostUrl + '/api/goods/exchangeGoods',
            method: 'post',
            data: {
                'goods_info': goodlist,
                'ship_id': that.data.chooseAddr.id,
                'remark':'',
                'sessionkey': wx.getStorageSync('sessionkey')
            },
            header: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                console.log(res);
                var errmsg = res.data.msg;
                var state = res.data.code;
                if (state == 200){
                    wx.showToast({
                        title: errmsg,
                        icon: "success",
                        duration: 2000,
                    });
                    setTimeout(function () {
                        wx.navigateTo({
                            url: '/pages/ord_all/ord_all?showType=0'
                        })
                    }, 1000);
                } else {
                    wx.showToast({
                        title: errmsg,
                        icon:"none",
                        duration: 2000,
                    });
                }
            },
            error: function (e) {
                wx.showToast({
                    title: '网络异常',
                    icon:"none",
                    duration: 2000,
                });
            },
        });



    }
})